﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace InterfaceFiles
{
  public class TextInterfaceWriter : IInterfaceWriter
  {
    FileStream stream;
    StreamWriter writer;
    StringBuilder currentRecord;

    public TextInterfaceWriter(string fileName)
    {
      stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read);
      writer = new StreamWriter(stream);
      currentRecord = new StringBuilder();
    }

    public string FileName
    {
      get;
      set;
    }

    public void WriteInt(int value)
    {
      currentRecord.AppendFormat("{0},", value);
    }

    public void WriteString(string value)
    {
      currentRecord.AppendFormat("\"{0}\",", value.Trim());
    }

    public void WriteDouble(double value)
    {
      currentRecord.AppendFormat("{0:F3},", value);
    }

    public void FlushRecord()
    {
      string stringToWrite = currentRecord.ToString().TrimEnd(new char[] {','});
      writer.WriteLine(stringToWrite);
      currentRecord.Clear();
    }
  }
}
